<!--系统基本信息-->
<template>
  <div class="video-list-manage-parent" ref="parentContainer">
    <!--操作表单区域-->
    <div class="option-parent" ref="optionParentContainer">
    </div>

    <!--数据表格区域-->
    <div v-bind:style="{height:tableHeight+'px',width:'100%',overflow:'auto'}" v-loading="loading">
      <el-form ref="form" label-width="auto" style="padding: 10px" label-position="center">
        <p class="item-title">基本信息:</p>
        <el-row :gutter="0">
          <el-col :span="5">
            <el-form-item label="代理名称:">
              {{agentInfo.agentname}}
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="持有人姓名:">
              {{agentInfo.agentowner_realname}}
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="银行名称:">
              {{genBankName(agentInfo.agentowner_bankcode)}}
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="银行账户:">
              {{agentInfo.agentowner_bankaccount}}
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="销售额:">
              {{agentInfo.agent_volume}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="0">
          <el-col :span="5">
            <el-form-item label="商户数量:">
              {{agentInfo.agent_sellers}}
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="骑手数量:">
              {{agentInfo.agent_riders}}
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="订单数量:">
              {{agentInfo.agent_orders}}
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="账户余额:">
              {{agentInfo.agentowner_acc}}
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button size="mini" type="warning" @click="getMoneyToBank()">提款</el-button>
          </el-col>
        </el-row>
        <p class="item-title">
          <span>班次:&nbsp;&nbsp;&nbsp;</span>
          <el-button size="mini" type="warning" @click="openEditor(1)">编辑</el-button>
          <el-button size="mini" type="warning" @click="modifyByWorkDay()" v-show="!canEditDuty">确认</el-button>
          <el-button size="mini" type="warning" @click="cancelEditor(1)" v-show="!canEditDuty">取消</el-button>
        </p>
        <el-row :gutter="0">
          <el-col :span="6">
            <el-form-item label="白班开始时间:">
              <el-time-picker
                :disabled="canEditDuty"
                v-model="agentInfo.deliver_work_day_start">
              </el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="白班结束时间:">
              <el-time-picker
                :disabled="canEditDuty"
                v-model="agentInfo.deliver_work_day_stop">
              </el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="夜班开始时间:">
              <el-time-picker
                :disabled="canEditDuty"
                v-model="agentInfo.deliver_work_night_start">
              </el-time-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="夜班结束时间:">
              <el-time-picker
                :disabled="canEditDuty"
                v-model="agentInfo.deliver_work_night_stop">
              </el-time-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <p class="item-title">
          <span>配送距离与费用:&nbsp;&nbsp;&nbsp;</span>
          <el-button size="mini" type="warning" @click="openEditor(2)">编辑</el-button>
          <el-button size="mini" type="warning" @click="modifyByDistance()" v-show="!canEditDistance">确认</el-button>
          <el-button size="mini" type="warning" @click="cancelEditor(2)" v-show="!canEditDistance">取消</el-button>
        </p>
        <el-row :gutter="0">
          <el-col :span="6">
            <el-form-item label="基本配送距离（米）:">
              <el-input
                :disabled="canEditDistance"
                style="width: auto"
                size="small"
                v-model="agentInfo.deliver_distance_start">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="基本配送费用(元):">
              <el-input
                :disabled="canEditDistance"
                style="width: auto"
                size="small"
                v-model="agentInfo.deliver_fee_start">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="基本配送时间(分):">
              <el-input
                :disabled="canEditDistance"
                style="width: auto"
                size="small"
                v-model="agentInfo.deliver_minute_start">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="最远配送距离:">
              <el-input
                :disabled="canEditDistance"
                style="width: auto"
                size="small"
                v-model="agentInfo.deliver_distance_stop">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="0" style="font-size: 14px;color: #606266;">
          <el-col :span="12">
            超过基本配送距离每
            <el-input
              :disabled="canEditDistance"
              style="width: auto"
              size="small"
              v-model="agentInfo.deliver_distance_extra">
            </el-input>
            米加1元配送费
          </el-col>
          <el-col :span="12">
            超过基本配送距离每
            <el-input
              :disabled="canEditDistance"
              style="width: auto"
              size="small"
              v-model="agentInfo.deliver_minute5_extra">
            </el-input>
            米加5分钟配送时间
          </el-col>
        </el-row>
        <p class="item-title" style="margin-top: 20px">
          <span>骑手时间分配:&nbsp;&nbsp;&nbsp;</span>
          <el-button size="mini" type="warning" @click="openEditor(3)">编辑</el-button>
          <el-button size="mini" type="warning" @click="modifyRiderTime()" v-show="!canEditRiderTime">确认</el-button>
          <el-button size="mini" type="warning" @click="cancelEditor(3)" v-show="!canEditRiderTime">取消</el-button>
        </p>
        <el-row style="font-size: 14px;color: #606266;">
          <el-col :span="24" style="margin-top: 20px">
            超过
            <el-input
              :disabled="canEditRiderTime"
              style="width: auto"
              size="small"
              v-model="agentInfo.deliver_rider_confirm_minutes">
            </el-input>
            分
            <el-input
              :disabled="canEditRiderTime"
              style="width: auto"
              size="small"
              v-model="agentInfo.deliver_rider_confirm_seconds">
            </el-input>
            秒
            骑手不接单重新分配
          </el-col>
        </el-row>
        <p class="item-title" style="margin-top: 20px">
          <span>优惠活动:&nbsp;&nbsp;&nbsp;</span>
          <el-button size="mini" type="warning" @click="openEditor(4)">编辑</el-button>
          <el-button size="mini" type="warning" @click="modifyPlusAction()" v-show="!canEditPlus">确认</el-button>
          <el-button size="mini" type="warning" @click="cancelEditor(4)" v-show="!canEditPlus">取消</el-button>
        </p>
        <el-row :gutter="0">
          <el-col :span="6">
            <el-form-item>
              <el-checkbox :disabled="canEditPlus" v-model="deliver_promotion_enable">有满减
              </el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="订单起步金额:" v-show="isShowPlus">
              <el-input
                :disabled="canEditPlus"
                style="width: auto"
                type="number"
                min="0.01"
                size="small"
                v-model="agentInfo.deliver_promotion_start">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="减配送费的金额:" v-show="isShowPlus">
              <el-input
                :disabled="canEditPlus"
                style="width: auto"
                type="number"
                min="0.01"
                size="small"
                v-model="agentInfo.deliver_promotion_amount">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
          </el-col>
        </el-row>
        <el-row :gutter="0">
          <el-col :span="6">
            <el-form-item label="开始时间:" v-show="isShowPlus">
              <el-date-picker
                :disabled="canEditPlus"
                size="small"
                v-model="agentInfo.deliver_promotion_dt1"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="结束时间:" v-show="isShowPlus">
              <el-date-picker
                :disabled="canEditPlus"
                size="small"
                v-model="agentInfo.deliver_promotion_dt2"
                type="datetime"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <!--翻页区域-->
    <div class="pagination-parent" ref="paginationParentContainer">

    </div>
  </div>
</template>

<script>
  import CPageBase from "../fragment/CPageBase"
  import CRootBase from "../fragment/CRootBase"

  export default {
    data() {
      return {
        pathArr: [
          {path: "/", name: "首页"},
          {path: "/", name: "系统信息"},
        ],
        agentInfo: "",//代理信息
        loading: false,

        canEditDuty: true,//班次是否可编辑
        canEditDistance: true,//配送距离是否可编辑
        canEditRiderTime: true,//重新分配骑手时间
        canEditPlus: true,//优惠活动
        isShowPlus: true,//是否显示优惠选项
        deliver_promotion_enable: false,

        deliver_work_day_start: "",//白班开始时间
        deliver_work_day_stop: "",//白班结束时间
        deliver_work_night_start: "",//夜班开始时间
        deliver_work_night_stop: "",//夜班结束时间

        deliver_promotion_dt1: "",//优惠开始时间
        deliver_promotion_dt2: ""//优惠结束时间
      }
    },
    mixins: [CPageBase, CRootBase],
    components: {},
    watch: {
      "deliver_promotion_enable": function () {
        if (this.deliver_promotion_enable) {
          this.isShowPlus = true;
        } else {
          this.isShowPlus = false;
        }
      }
    },
    mounted() {
    },
    activated() {
      this.initCurrentPath();
      this.requestAgentSellerSummary();
    },
    deactivated() {
      this.$destroy(true);
    },
    methods: {
      //初始化面包屑路径
      initCurrentPath() {
        this.$store.commit('changeCurrentLocation', this.pathArr);
      },

      //获取个人信息
      requestAgentSellerSummary() {
        let parameter = new URLSearchParams();
        parameter.append("action", "agent_view");
        let that = this;
        that.loading = true;
        this.$http.requestAuthData(that, parameter, function (data) {
          that.agentInfo = data;
          that.agentInfo.deliver_promotion_dt1 = that.formatDateTime(data.deliver_promotion_dt1);
          that.agentInfo.deliver_promotion_dt2 = that.formatDateTime(data.deliver_promotion_dt2);

          that.agentInfo.deliver_work_day_start = that.formatTime(data.deliver_work_day_start);
          that.agentInfo.deliver_work_day_stop = that.formatTime(data.deliver_work_day_stop);
          that.agentInfo.deliver_work_night_start = that.formatTime(data.deliver_work_night_start);
          that.agentInfo.deliver_work_night_stop = that.formatTime(data.deliver_work_night_stop);
          if (that.agentInfo.deliver_promotion_enable === "1") {
            that.deliver_promotion_enable = true;
            that.isShowPlus = true;
          } else {
            that.deliver_promotion_enable = false;
            that.isShowPlus = false;
          }
        });
      },

      //格式日期时间格式
      formatDateTime(dateStr) {
        return new Date(dateStr.substring(0, 4),
          parseInt(dateStr.substring(5, 7)) - 1,
          dateStr.substring(8, 10),
          dateStr.substring(11, 13),
          dateStr.substring(14, 16),
          dateStr.substring(17, 19)
        );
      },

      //格式化时间值
      formatTime(timeStr) {
        let date = new Date();
        date.setHours(timeStr.substring(0, 2));
        date.setMinutes(timeStr.substring(3, 5));
        date.setSeconds(timeStr.substring(6, 8));
        return date;
      },

      //打开编辑模式
      openEditor(sign) {
        switch (sign) {
          case 1:
            this.canEditDuty = false;
            this.canEditDistance = true;
            this.canEditRiderTime = true;
            this.canEditPlus = true;
            break;
          case 2:
            this.canEditDuty = true;
            this.canEditDistance = false;
            this.canEditRiderTime = true;
            this.canEditPlus = true;
            break;
          case 3:
            this.canEditDuty = true;
            this.canEditDistance = true;
            this.canEditRiderTime = false;
            this.canEditPlus = true;
            break;
          case 4:
            this.canEditDuty = true;
            this.canEditDistance = true;
            this.canEditRiderTime = true;
            this.canEditPlus = false;
            break;
        }
        this.requestAgentSellerSummary();
      },

      //取消编辑模式
      cancelEditor(sign) {
        switch (sign) {
          case 1:
            this.canEditDuty = true;
            break;
          case 2:
            this.canEditDistance = true;
            break;
          case 3:
            this.canEditRiderTime = true;
            break;
          case 4:
            this.canEditPlus = true;
            break;
        }
      },

      //将时间转为字符格式
      formatTime2Str(date) {
        let time = "";
        if (date.getHours() < 10) {
          time = time + "0" + date.getHours();
        } else {
          time = time + date.getHours();
        }

        if (date.getMinutes() < 10) {
          time = time + ":0" + date.getMinutes();
        } else {
          time = time + ":" + date.getMinutes();
        }

        if (date.getSeconds() < 10) {
          time = time + ":0" + date.getSeconds();
        } else {
          time = time + ":" + date.getSeconds();
        }
        return time;
      },

      //将日期转为字符格式
      formatDate2Str(date) {
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let hours = date.getHours();
        let minutes = date.getMinutes();
        let seconde = date.getSeconds();

        let time = year;
        if (month < 10) {
          time = time + "-0" + month;
        } else {
          time = time + "-" + month;
        }
        if (day < 10) {
          time = time + "-0" + day;
        } else {
          time = time + "-" + day;
        }
        if (hours < 10) {
          time = time + " 0" + hours;
        } else {
          time = time + " " + hours;
        }
        if (minutes < 10) {
          time = time + ":0" + minutes;
        } else {
          time = time + ":" + minutes;
        }
        if (seconde < 10) {
          time = time + ":0" + seconde;
        } else {
          time = time + ":" + seconde;
        }
        return time;
      },


      //修改班次时间
      modifyByWorkDay() {
        let dataStart = this.formatTime2Str(this.agentInfo.deliver_work_day_start);
        let dataStop = this.formatTime2Str(this.agentInfo.deliver_work_day_stop);
        let nightStart = this.formatTime2Str(this.agentInfo.deliver_work_night_start);
        let nightStop = this.formatTime2Str(this.agentInfo.deliver_work_night_stop);
        let parameter = new URLSearchParams();
        parameter.append("action", "agent_modify");
        parameter.append("deliver_work_day_start", dataStart);
        parameter.append("deliver_work_day_stop", dataStop);
        parameter.append("deliver_work_night_start", nightStart);
        parameter.append("deliver_work_night_stop", nightStop);
        let that = this;
        that.loading = true;
        this.$http.requestAuthData(that, parameter, function (data) {
          that.requestAgentSellerSummary();
          that.canEditDuty = true;
        });
      },

      //修改配送距离
      modifyByDistance() {
        let dataStart = this.formatTime2Str(this.agentInfo.deliver_work_day_start);
        let dataStop = this.formatTime2Str(this.agentInfo.deliver_work_day_stop);
        let nightStart = this.formatTime2Str(this.agentInfo.deliver_work_night_start);
        let nightStop = this.formatTime2Str(this.agentInfo.deliver_work_night_stop);
        let parameter = new URLSearchParams();
        parameter.append("action", "agent_modify");
        parameter.append("deliver_fee_start", this.agentInfo.deliver_fee_start);
        parameter.append("deliver_distance_start", this.agentInfo.deliver_distance_start);
        parameter.append("deliver_minute_start", this.agentInfo.deliver_minute_start);
        parameter.append("deliver_distance_stop", this.agentInfo.deliver_distance_stop);
        parameter.append("deliver_distance_extra", this.agentInfo.deliver_distance_extra);
        parameter.append("deliver_minute5_extra", this.agentInfo.deliver_minute5_extra);
        let that = this;
        that.loading = true;
        this.$http.requestAuthData(that, parameter, function (data) {
          that.requestAgentSellerSummary();
          that.canEditDistance = true;
        });
      },

      //修改骑手未接单重新分配时间
      modifyRiderTime() {
        let parameter = new URLSearchParams();
        parameter.append("action", "agent_modify");
        parameter.append("deliver_rider_confirm_minutes", this.agentInfo.deliver_rider_confirm_minutes);
        parameter.append("deliver_rider_confirm_seconds", this.agentInfo.deliver_rider_confirm_seconds);
        let that = this;
        that.loading = true;
        this.$http.requestAuthData(that, parameter, function (data) {
          that.requestAgentSellerSummary();
          that.canEditRiderTime = true;
        });
      },

      //修改优惠活动
      modifyPlusAction() {
        let parameter = new URLSearchParams();
        let enable = "";
        if (this.deliver_promotion_enable) {
          enable = "1";
          parameter.append("deliver_promotion_enable", enable);
          if (parseFloat(this.agentInfo.deliver_promotion_start) < 0.01) {
            this.$message.error("订单起步金额不能小于0.01");
            return;
          }
          if (parseFloat(this.agentInfo.deliver_promotion_amount) < 0.01) {
            this.$message.error("减配送费的金额:不能小于0.01");
            return;
          }
          parameter.append("deliver_promotion_start", this.agentInfo.deliver_promotion_start);
          parameter.append("deliver_promotion_amount", this.agentInfo.deliver_promotion_amount);
          parameter.append("deliver_promotion_dt1", this.formatDate2Str(this.agentInfo.deliver_promotion_dt1));
          parameter.append("deliver_promotion_dt2", this.formatDate2Str(this.agentInfo.deliver_promotion_dt2));
        } else {
          enable = "0";
          parameter.append("deliver_promotion_enable", enable);
        }
        parameter.append("action", "agent_modify");
        let that = this;
        that.loading = true;
        this.$http.requestAuthData(that, parameter, function (data) {
          that.requestAgentSellerSummary();
          that.canEditPlus = true;
        });
      },


      //提款金额输入对话框
      getMoneyToBank() {
        let that = this;
        this.$prompt('请输入提款金额', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({value}) => {
          that.computerServiceCharge(value);
        }).catch(() => {
        });
      },

      //计算提款手续费
      computerServiceCharge(money) {
        if (this.$utils.isEmpty(money) || isNaN(money) || parseFloat(money) >parseFloat(this.agentInfo.agentowner_acc)) {
          this.$message({
            message: "未输入提款金额或提款金额大于账户余额"
          });
          return;
        }
        let parameter = new URLSearchParams();
        parameter.append("action", "info_weixin_transfer_fee");
        parameter.append("amount", money);
        let that = this;
        that.loading = true;
        this.$http.requestAuthData(that, parameter, function (data) {
          that.$confirm("本次提款金额:" + money + "元,手续费:" + data.total_fee + "元,实际到账:" + (parseFloat(money) - parseFloat(data.total_fee)) + "元", '提示', {
            confirmButtonText: '确定提款',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            that.withdrawMoney(money);
          }).catch(() => {
          });
        });
      },

      //发起提款请求
      withdrawMoney(money) {
        let parameter = new URLSearchParams();
        parameter.append("action", "agent_withdraw");
        parameter.append("amount", money);
        let that = this;
        that.loading = true;
        this.$http.requestAuthData(that, parameter, function (data) {
          that.$message({
            message: "您的提款请求已成功提交!"
          });
          that.requestAgentSellerSummary();
        });
      },

      // 获取银行名称
      genBankName (code) {
        let item = localStorage.getItem("systerm_info");
        let info = JSON.parse(item);
        let banks = info.bank_weixin;
        let codes = banks.code;
        let names = banks.name;
        for (let i = 0; i < codes.length; i++) {
          if (code === codes[i]) {
            return names[i];
          }
        }
      }

    }
  }
</script>

<style lang="scss" scoped="" type="text/scss" src="../../assets/style/base-info.scss">
</style>

